#include "types.h"
#include "stat.h"
#include "user.h"
//#include "fs.h"


int
main(int argc, char *argv[])
{
	
	int i;
	int pid;
	int cid;
	int j;
	int wtime;
	int rtime;
	int wtimes[30];
	int rtimes[30];
	int avgwtime=0;
	int avgrtime=0;
	int avgttime=0;
	int counter;
	int ttime;

	for (i=0; i<30; i++){
		if((pid=fork())==0) {
			cid = i;
			if( (cid % 2) == 0){
				nice();
			}
			for (j=0; j< 500; j++){
				printf(1,"My CID is: %d\n", cid);
			}



		//break;
			exit();

		}
	}


	if (pid != 0){
		for (counter = 0; counter<30; counter++){
			wait2(&wtime, &rtime);
			//printf(1, "wtime: %d ; rtime: %d\n", wtime1, rtime1);
			wtimes[counter] = wtime;
			rtimes[counter] = rtime;
			
			//printf(1, "test %d waiting time: %d , %d\n", counter,wtimes[counter], rtimes[counter]);
		}
		
		for (counter = 0; counter<30; counter++){
			avgwtime = avgwtime + wtimes[counter];
			avgrtime = avgrtime + rtimes[counter];
			avgttime = avgttime + rtimes[counter] + wtimes[counter];
		}
		
		avgwtime = avgwtime / 30;
		avgrtime = avgrtime / 30;
		avgttime = avgttime / 30;
		
		printf(1, "Average waiting time: %d\n", avgwtime);
		printf(1, "Average running time: %d\n", avgrtime);
		printf(1, "Average turnaround time: %d\n", avgttime);
		
		for (counter = 0; counter<30; counter++){
			ttime = wtimes[counter] + rtimes[counter];
			printf(1, "Waiting: %d , Running: %d , Turnaround: %d \n", wtimes[counter], rtimes[counter], ttime);
		}
		
    }    
  exit();
}
